import {ajax,uploadFile} from 'global.js';
import power from 'directive/power.js';
import {toast} from '../bi-plug/plug.js';
import {assign} from '../bi-plug/global.js';

import AdminPageHeader from '../component/page-header.vue';
import AdminPageWelcome from '../component/page-welcome.vue';
import AdminSiderMenu from '../component/sider-menu.vue';
import AdminSiderGroup from '../component/sider-group.vue';
import AdminUserGroup from '../component/field/user-group.vue';
import AdminEditor from '../component/field/admin-editor.vue';
import AdminSystemFile from '../component/field/admin-system-file.vue';
let components = {
    AdminPageHeader,
    AdminPageWelcome,
    AdminSiderMenu,
    AdminSiderGroup,
    AdminUserGroup,
    AdminEditor,
    AdminSystemFile
}

let directives = {
    power,
}

export default {
    install(app,object) {
    
        for(let name in components){
             app.component(name,components[name]);
        } 
        
        for(let name in directives) {
            app.directive(name,directives[name]);
        }
        
        app.config.globalProperties.$ajax = ajax;
        app.config.globalProperties.$url  = (config) =>{
          return  object.url(config);
        };

        app.config.globalProperties.$go = (config) =>{
          return  object.go(config);
        };
        
        app.config.globalProperties.$pageData = object.data;
        app.config.globalProperties.$user = object.user;
        app.config.globalProperties.$admin = object;
        app.config.globalProperties.$status = object.status;

        app.config.globalProperties.$appToast = (message,app = '' , callback = ()=>{}) =>{

            if (app=='') app = object.app;
            appInfo = object.getAppInfo(app);
            let config = {
                icon: appInfo['icon'],
                title: appInfo['title'],
                type: '',
                time:new Date().getTime(),
                text:'',
                autoClose:3000
            }

            if (typeof message ==='string'){
                config.text = message;
            }else{
                assign(config,message);
            }

            toast(config,callback);
        }


        //app.config.globalProperties.$status =  new status()



        window.__bi_file_upload = (file,params,callback) =>{
            let url = object.url({app:'admin',controller:'file',action:'upload'});
            uploadFile(url,file,params,callback);
        }

        window.__bi_file_open = (callback,ext ='',app = '')=>{
            object.userFile(callback,ext,app);
        };

        window.__bi_upload_url = (file) =>{
            return object.uploadBaseUrl + file;
        }


        app.config.globalProperties.$uploadUrl = (file)=>{
            return object.uploadBaseUrl + file;
        }


        window.__bi_file_info = (file,callback) =>{
            ajax({
                url:object.url({app:'admin',controller:'file',action:'info'}),
                data:{
                    file:file
                },
                success:callback
            });
        }

        window.__bi_file_saveinfo = (file,data,callback)=>{
            ajax({
                url:object.url({app:'admin',controller:'file',action:'saveinfo'}),
                data:{
                    file:file,
                    ...data
                },
                success:callback
            });
        }

    }
}